package com.frogs.ucenter.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.entity.UserMessageDmReceiver;
import com.frogs.ucenter.service.UserAccountInfoService;
import com.frogs.ucenter.service.UserMessageDmReceiverService;
@Controller
@RequestMapping("/admin/userMessageDmReceiver")
public class UserMessageDmReceiverController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userMessageDmReceiver";

    private static final String HOME_VIEW = "admin/userMessageDmReceiver/home";

    private static final String EDIT_VIEW = "admin/userMessageDmReceiver/edit";
    
    private static final String USER_LIST = "/userList";
    
    private static final String USER_SAVE = "/userSave";
    
    private static final String USER_LIST_VIEW = "admin/userMessageDmReceiver/userList";
    
    private static final String RECEIVER_LIST_VIEW = "admin/userMessageDmReceiver/receiverList";
    
    @Autowired
    private UserMessageDmReceiverService userMessageDmReceiverService;
    @Resource
    private UserAccountInfoService userAccountInfoService;

    @RequestMapping()
    public String listUserMessageDmReceiver(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserMessageDmReceiver(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",userMessageDmReceiverService.getById( id ) ); 
        }
        int type = reqInfo.getIntParameter("type", 0);
        if(type==1){
        	return RECEIVER_LIST_VIEW;
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserMessageDmReceiver(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserMessageDmReceiver UserMessageDmReceiver) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
        	userMessageDmReceiverService.insert( UserMessageDmReceiver ); 
            builder = DWZResponse.getSucessBuilder("新增成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserMessageDmReceiver(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( userMessageDmReceiverService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserMessageDmReceiver(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserMessageDmReceiver userRechargeCheckNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                UserMessageDmReceiver UserMessageDmReceiver = userMessageDmReceiverService.getById( id ); 
                PropertyUtils.copyProperties(UserMessageDmReceiver, userRechargeCheckNew);
                userMessageDmReceiverService.update( UserMessageDmReceiver ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserMessageDmReceiver(HttpServletRequest request, HttpServletResponse response, ModelMap model, @RequestParam(required=true, value="ids") String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userMessageDmReceiverService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除信息成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("删除信息失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }
    @RequestMapping(USER_SAVE)
    public String addReceiver(HttpServletRequest request, HttpServletResponse response, ModelMap model, @RequestParam(required=true, value="ids") String inIdList) {
    	HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    	setRequestModelMap(request, model);
    	DWZResponse.Builder builder;
    	try {
    		String[] idArray = inIdList.split(",");
    		int dmId = reqInfo.getIntParameter("dmId");
    		List<UserMessageDmReceiver> userList = new ArrayList<UserMessageDmReceiver>();
    		for(int i=0;i<idArray.length;i++){
    			UserMessageDmReceiver user = new UserMessageDmReceiver();
    			String userId = idArray[i].split(":;")[0];
    			String receiver = idArray[i].split(":;")[1];
    			user.setDmId(dmId);
    			user.setReceiver(receiver);
    			user.setUserId(userId);
    			userList.add(user);
    		}
    		userMessageDmReceiverService.batchInsert(userList);
    		builder = DWZResponse.getSucessBuilder("添加收件人成功");
    	} catch (Exception e) {
    		builder = DWZResponse.getFailBuilder("添加收件人失败" + e.getMessage());
    	}
    	RenderUtil.renderHtml(builder.build().toString(), response);
    	model.clear();
    	return null;
    }

    private PageList<UserMessageDmReceiver> getPageList(ModelMap model) {
        PageList<UserMessageDmReceiver> userMessageDmReceiverList = userMessageDmReceiverService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userMessageDmReceiverList",userMessageDmReceiverList);
        model.put("pageTurn",userMessageDmReceiverList.getPageTurn());
        return userMessageDmReceiverList;
    }
    
    @RequestMapping(USER_LIST)
    public String listUser(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getUserList( model );
        return USER_LIST_VIEW;
    }
    
    private PageList<UserBaseAccountInfo> getUserList(ModelMap model) {
        PageList<UserBaseAccountInfo> userAccountInfoList = userAccountInfoService.listBaseAccount( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userBaseAccountInfoList",userAccountInfoList);
        model.put("pageTurn",userAccountInfoList.getPageTurn());
        return userAccountInfoList;
    }
}
